package com.base.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.service.SubscribersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.VersionHistory;
import com.base.service.UsersService;
import com.base.service.VersionHistoryService;

/**
 * 历史版本
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/app_versionhistory")
public class app_VersionHistoryController {
	@Autowired
	private SubscribersService subscribersService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private VersionHistoryService versionHistoryService;

	

	// 历史版本
	@RequestMapping(value = "/getVersionHistory", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView getVersionhistory(HttpServletRequest request, HttpServletResponse response,
			String Mobile, String ParentId, Byte type) throws Exception {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		PrintWriter out = response.getWriter();

		//TODO 判断参数
		if(ParentId == null || ParentId.equals("")) {
			out.print("参数异常");
			System.out.println("参数异常");
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		
		//取到手机号用户的id
		List<String> subscribersId = subscribersService.selectByMobileAndParentId(Mobile, ParentId);
		

		
		if(subscribersId != null && subscribersId.size()>0) {
			//手机号所有信息
//			Subscribers subscribers = subscribersService.selectByPrimaryKey(subscribersId.get(0));
		}else {
			System.out.println("未查询到用户信息");
			out.print("未查询到用户信息");
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		
		List<VersionHistory> versionHistory = versionHistoryService.selectByType(type, ParentId);

        if(versionHistory != null) {
        	
        	request.setAttribute("list", versionHistory);
        	modelAndView.setViewName("jsp/app_VersionHistory");
        	return modelAndView;
        }
        modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	@RequestMapping("/app_VersionHistoryDetails")
	public ModelAndView index(HttpServletRequest request,String id) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		
		VersionHistory versionHistory = versionHistoryService.selectByPrimaryKey(id);
		if(versionHistory != null) {
			
			request.setAttribute("id", id);
			request.setAttribute("content", versionHistory.getContent());
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/app_VersionHistoryDetails");
			return modelAndView;
		}
		
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
}